package com.chaoyue.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chaoyue.base.exception.ChaoYuePlusException;
import com.chaoyue.base.utils.ObjectUtils;
import com.chaoyue.content.beans.dto.BindTeachplanMediaDto;
import com.chaoyue.content.beans.dto.SaveTeachplanDto;
import com.chaoyue.content.beans.dto.TeachplanDto;
import com.chaoyue.content.beans.po.Teachplan;
import com.chaoyue.content.beans.po.TeachplanMedia;
import com.chaoyue.content.mapper.TeachplanMapper;
import com.chaoyue.content.mapper.TeachplanMediaMapper;
import com.chaoyue.content.service.TeachplanService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@SuppressWarnings({"all"})
/**
 * 课程计划 服务实现类
 * @author whr
 */
@Slf4j
@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachplanService {
    @Autowired
    TeachplanMapper teachplanMapper;
    @Autowired
    TeachplanMediaMapper teachplanMediaMapper;

    public List<TeachplanDto> findTeachplanTree(Long courseId) {
        List<TeachplanDto> teachplanDtos = teachplanMapper.selectTreeNodes(courseId);
        return teachplanDtos;
    }

    @Override
    public void saveTeachplan(SaveTeachplanDto saveTeachplanDto) {

        //根据id判断前端是修改还是新增操作
        Long id = saveTeachplanDto.getId();
        if (ObjectUtils.isEmpty(id)) {
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(saveTeachplanDto, teachplan);
            Long courseId = saveTeachplanDto.getCourseId();
            Long parentId = saveTeachplanDto.getParentid();
            LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();

            queryWrapper.eq(ObjectUtils.isNotEmpty(courseId), Teachplan::getCourseId, courseId)
                    .eq(ObjectUtils.isNotEmpty(parentId), Teachplan::getParentid, parentId);
            Integer count = teachplanMapper.selectCount(queryWrapper);
            teachplan.setOrderby(count + 1);
            teachplanMapper.insert(teachplan);
        } else {
            Teachplan teachplan = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(saveTeachplanDto, teachplan);
            teachplanMapper.updateById(teachplan);
        }
    }

    @Transactional
    @Override
    public void associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if (ObjectUtils.isEmpty(teachplan)) {
            ChaoYuePlusException.cast("课程计划不存在");
        }
        //删除课程计划绑定的媒资
        LambdaQueryWrapper<TeachplanMedia> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeachplanMedia::getTeachplanId, teachplanId);
        teachplanMediaMapper.delete(queryWrapper);
        //插入新的媒资关联关系
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        BeanUtils.copyProperties(bindTeachplanMediaDto, teachplanMedia);
        teachplanMedia.setCourseId(teachplan.getCourseId());
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMediaMapper.insert(teachplanMedia);
    }
}
